
#include "stdio.h"
#include "stdlib.h"
#include "systick.h"
#include "../User/BSP/BUZZER/buzzer.h"
#include "../User/BSP/TIMER1/timer.h"

Buzzer_Struct Buzzer;


uint32_t BuzzerInsertRing[2][BuzzerRingGroup] = {0};
/*!
    \brief      vbuzzerSetRing
    \param[in]  time
		\param[in]  speed
    \param[out] none
    \retval     none
*/
void vbuzzerSetRing(uint32_t time, uint32_t speed)
{

    vbuzzerCopyArray_32(Buzzer.config.Info.buzzerTime, &BuzzerInsertRing[0][1], BuzzerRingGroup - 1);
    vbuzzerCopyArray_32(Buzzer.config.Info.buzzerSpeed, &BuzzerInsertRing[1][1], BuzzerRingGroup - 1);
    BuzzerInsertRing[0][0] = (time + 1) * 2;
    BuzzerInsertRing[1][0] = speed;
    vbuzzerCopyArray_32(BuzzerInsertRing[0], Buzzer.config.Info.buzzerTime, BuzzerRingGroup);
    vbuzzerCopyArray_32(BuzzerInsertRing[1], Buzzer.config.Info.buzzerSpeed, BuzzerRingGroup);
    Buzzer.config.Info.group++;
}

bool buzzerRingFlag = true;
/*!
    \brief      vbuzzerRing
    \param[in]  status
    \param[out] none
    \retval     none
*/
void vbuzzerRing(void)
{

    if((Buzzer.control.enable) && (Buzzer.config.Info.group > 0))
    {
        if(Buzzer.config.Info.buzzerTime[Buzzer.config.Info.group - 1] > 1)
        {
            if(vtimerIsTimeOut_ms(&Buzzer.config.Info.buzzerRingTime, Buzzer.config.Info.buzzerSpeed[Buzzer.config.Info.group - 1]))
            {
                //Toggle
                buzzerRingFlag ? gpio_bit_set(BUZZER_GPIOx, BUZZER_GPIO_PINx) : gpio_bit_reset(BUZZER_GPIOx, BUZZER_GPIO_PINx);
                buzzerRingFlag = !buzzerRingFlag;
                //Time
                Buzzer.config.Info.buzzerTime[Buzzer.config.Info.group - 1]--;

                Buzzer.config.Info.buzzerRingTime = vtimerGetCurrentTime_ms();
            }
            else
            {

            }
        }
        else if(Buzzer.config.Info.buzzerTime[Buzzer.config.Info.group - 1] == 1)
        {
            gpio_bit_reset(BUZZER_GPIOx, BUZZER_GPIO_PINx);
            Buzzer.config.Info.buzzerTime[Buzzer.config.Info.group - 1]--;
        }
        else
        {
            Buzzer.config.Info.group--;
        }
    }
}

/*!
    \brief      vbuzzerInit
    \param[in]  none
    \param[out] none
    \retval     none
*/
void vbuzzerInit(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);
    gpio_init(BUZZER_GPIOx, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, BUZZER_GPIO_PINx);
    gpio_bit_reset(BUZZER_GPIOx, BUZZER_GPIO_PINx);

    Buzzer.control.enable = true;
    Buzzer.control.Ring = vbuzzerRing;
    Buzzer.control.SetRing = vbuzzerSetRing;

    //		rcu_periph_clock_enable(RCU_GPIOB);
    //    gpio_init(BUZZER_GPIOx, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, BUZZER_GPIO_PINx);
    //
    //    timer_oc_parameter_struct timer_ocintpara;
    //    timer_parameter_struct timer_initpara;
    //    rcu_periph_clock_enable(RCU_TIMER9);
    //    timer_deinit(TIMER9);
    //    /* LED_TIMERx configuration */
    //    timer_initpara.prescaler         = 0;
    //    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    //    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    //    timer_initpara.period            = 0x7FF;
    //    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    //    timer_initpara.repetitioncounter = 0;
    //    timer_init(TIMER9, &timer_initpara);
    //    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    //    timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
    //    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    //    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    //    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    //    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    //    timer_channel_output_config(TIMER9, TIMER_CH_0, &timer_ocintpara);
    //    timer_channel_output_pulse_value_config(TIMER9, TIMER_CH_0, 0);//pwmdata
    //    timer_channel_output_mode_config(TIMER9, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    //    timer_channel_output_shadow_config(TIMER9, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
    //    timer_primary_output_config(TIMER9, ENABLE);
    //		nvic_irq_enable(TIMER2_IRQn, 1, 0);
    //		timer_interrupt_enable(TIMER9,TIMER_INT_UP);

    //    timer_auto_reload_shadow_enable(TIMER9);
    //    timer_enable(TIMER9);
    //

}

void vbuzzerCopyArray_32(uint32_t *sourcearray, uint32_t *targetarray, uint32_t length)
{
    for(uint32_t i = 0; i < length; i++)
    {
        targetarray[i] = sourcearray[i];
    }
}
